
CREATE  trigger tt_billlay_bu
on dbo.tt_billlay
instead of update
as  
begin
  set nocount on
  declare @new_tid integer, @new_layid integer, @new_quantity decimal(18,4), @old_tid integer,
    @old_layid integer, @new_assqty decimal(18,4),@new_depotid integer,@new_productid integer,
    @old_depotid integer, @old_productid integer 
    
  declare cur1 cursor local forward_only optimistic for
  select tid, layid, bquantity, assqty, depotid, productid from inserted
  
  declare cur2 cursor local forward_only optimistic for
  select tid, layid, depotid, productid from deleted
  
  open cur1
  open cur2
  fetch next from cur1 into @new_tid, @new_layid, @new_quantity,@new_assqty,@new_depotid,@new_productid
  fetch next from cur2 into @old_tid, @old_layid, @old_depotid,@old_productid  
  
  while (@@fetch_status = 0)
  begin
    update tt_billlay 
      set tid=@new_tid,layid=@new_layid,bquantity=@new_quantity, assqty = @new_assqty,
       depotid = @new_depotid, productid = @new_productid
      where layid=@old_layid and depotid = @old_depotid and productid = @old_productid
    fetch next from cur1 into @new_tid, @new_layid, @new_quantity,@new_assqty,@new_depotid,@new_productid
    fetch next from cur2 into @old_tid, @old_layid,@old_depotid,@old_productid    
  end
  
  close cur1
  deallocate cur1
  close cur2
  deallocate cur2
end

